<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
          <el-form-item label="接口名称" prop="callInterfaceName">
            <el-input v-model.trim="queryParams.callInterfaceName" maxlength="200" clearable placeholder="请输入接口名称" class="filter-item limit" @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-form>
        <div class="flex-1"></div>
        <div>
          <div class="flex-1"></div>
          <div style="display:flex;">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
          </div>
        </div>
      </div>
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-permission="['INTERFACEINFO_ADD_BUTTON']"
          >新建</el-button>
          <div class="flex-1" ></div>
          <dt-dialog-column v-model="isShowTable" :columns="columns" :table-ref="$refs.table" @queryTable="getList" />
        </div>
        <el-table v-if="isShowTable" v-loading="loading" :data="interfaceInfoList" ref="table" border :header-cell-style="headerCellStyle">
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <!-- <el-table-column fixed="left" type="index" label="序号" width="70" :index="(index)=>(queryParams.pageNo - 1) * queryParams.pageSize + index + 1" /> -->
          <el-table-column label="接口名称" v-if="columns[0].show" show-overflow-tooltip key="callInterfaceName" align="center" prop="callInterfaceName" min-width="130px"/>
          <el-table-column label="接口编码" v-if="columns[1].show" show-overflow-tooltip key="interfaceCode" align="center" prop="interfaceCode" min-width="130px"/>
          <el-table-column label="接口地址" v-if="columns[2].show" show-overflow-tooltip key="interfacePath" align="center" prop="interfacePath" min-width="410px"/>
          <el-table-column label="信息查询间隔(天)" v-if="columns[3].show" show-overflow-tooltip key="intervalDays" align="center" prop="intervalDays" min-width="130px"/>
          <el-table-column label="是否分页" v-if="columns[4].show" show-overflow-tooltip key="isPage" align="center" prop="isPage" min-width="110px">
            <template slot-scope="scope">{{isPageFormat(scope.row.isPage)}}</template>
          </el-table-column>
          <el-table-column label="详情集合字段" v-if="columns[5].show" show-overflow-tooltip key="detailField" align="center" prop="detailField" min-width="130px"/>
          <el-table-column label="分页详情唯一标识" v-if="columns[6].show" show-overflow-tooltip key="detailFileldMark" align="center" prop="detailFileldMark" min-width="100px"/>
          <el-table-column label="操作" :key="Math.random()" fixed="right" align="center" class-name="small-padding fixed-width" width="120px">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleUpdate(scope.row)"
                v-permission="['INTERFACEINFO_UPDATE_BUTTON']"
              >修改</el-button>
              <el-button
                type="text"
                @click="handleDelete(scope.row)"
                style="color: red"
                v-permission="['INTERFACEINFO_DEL_BUTTON']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <dt-pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 添加或修改企业查询调用接口信息对话框 -->
    <dt-dialog :title="title" :visible.sync="open" :is-loading="loading" @comfirmBtn="submitForm" @closeDialog="cancel">
      <div slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
            <el-form-item label="接口名称" prop="callInterfaceName">
            <el-input v-model.trim="form.callInterfaceName" placeholder="请输入接口名称" maxlength="30" show-word-limit class="limit" />
          </el-form-item>
          <el-form-item label="接口地址" prop="interfacePath">
            <el-input v-model.trim="form.interfacePath" maxlength="200" placeholder="请输入内容" show-word-limit class="limit" />
          </el-form-item>
          <el-form-item label="是否分页" prop="isPage">
            <el-radio-group v-model.trim="form.isPage">
              <el-radio v-for="item in isPageList" :key="item.id" :label="item.value">{{item.name}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="信息查询间隔(天)" prop="intervalDays">
            <el-input-number v-model.trim="form.intervalDays" controls-position="right" placeholder="请输入数字" :min="0" :step="1" step-strictly show-word-limit class="limit" />
          </el-form-item>
          <el-form-item label="详情集合字段" prop="detailField" v-if="form.isPage === '2'">
            <el-input v-model.trim="form.detailField" placeholder="请输入详情字段" maxlength="30" show-word-limit class="limit" />
          </el-form-item>
          <el-form-item label="分页详情唯一标识" prop="detailFileldMark" v-if="form.isPage === '2'">
            <el-input v-model.trim="form.detailFileldMark" placeholder="请输入分页详情唯一标示" maxlength="30" show-word-limit class="limit" />
          </el-form-item>
            
        </el-form>
      </div>
    </dt-dialog>
  </div>
</template>

<script>
import { listInterfaceInfo, delInterfaceInfo, addInterfaceInfo, updateInterfaceInfo } from "@/api/interfaceInfo/InterfaceInfo";
import dialogColumn from '@/framework/utils/mixins/dialogColumn'
export default {
  name: "InterfaceInfo",
  mixins: [dialogColumn],
  data() {
    return {
      // 遮罩层
      loading: false,
      //显隐表格
      isShowTable: true,
      // 选中数组
      ids: [],
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      //列显隐数组
      columns: [
        { prop: "callInterfaceName", label: "接口名称", show: true},
        { prop: "interfaceCode", label: "接口编码", show: true},
        { prop: "interfacePath", label: "接口地址", show: true},
        { prop: "intervalDays", label: "信息查询间隔(天)", show: true},
        { prop: "isPage", label: "是否分页", show: true},
        { prop: "detailField", label: "详情集合字段", show: true},
        { prop: "detailFileldMark", label: "分页详情唯一标识", show: true}
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业查询调用接口信息表格数据
      interfaceInfoList: [],
      // 是否分页枚举
      isPageList: [
        { id: 1, name: '分页', value: '2' },
        { id: 2, name: '不分页', value: '1' }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        callInterfaceName: null
      },
      // 表单参数
      form: {
        id: null,
        callInterfaceName: null,
        interfaceCode: null,
        interfacePath: null,
        intervalDays: null,
        isPage: '1',
        detailField: null,
        detailFileldMark: null
      },
      callInterfaceName: null,
      // 表单校验
      rules: {
        callInterfaceName: [
          { required: true, message: '请输入接口名称', trigger: 'blur' },
          { required: true, message: '请输入接口名称', trigger: 'change' }
        ],
        interfacePath: [
          { required: true, message: '请输入接口地址', trigger: 'blur' },
          { required: true, message: '请输入接口地址', trigger: 'change' }
        ],
        isPage: [
          { required: true, message: '请选择是否分页', trigger: 'blur' },
          { required: true, message: '请选择是否分页', trigger: 'change' }
        ],
        intervalDays: [
          { required: true, message: '请输入信息查询间隔(天)', trigger: 'blur' },
          { required: true, message: '请输入信息查询间隔(天)', trigger: 'change' }
        ],
        detailField: [
          { required: true, message: '请输入详情集合字段', trigger: 'blur' },
          { required: true, message: '请输入详情集合字段', trigger: 'change' }
        ],
        detailFileldMark: [
          { required: true, message: '请输入分页详情唯一标示', trigger: 'blur' },
          { required: true, message: '请输入分页详情唯一标示', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业查询调用接口信息列表 */
    getList() {
      this.loading = true;
      listInterfaceInfo(this.queryParams).then(({ data: response }) => {
        this.interfaceInfoList = response.rows;
        this.total = response.totalRows;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    // 转换是否分页
    isPageFormat(isPage) {
      var result = '';
      if (isPage === '1') {
        result = '不分页';
      } else if (isPage === '2') {
        result = '分页';
      }
      return result;
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        callInterfaceName: null,
        interfaceCode: null,
        interfacePath: null,
        intervalDays: null,
        isPage: '1',
        detailField: null,
        detailFileldMark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增企业查询调用接口信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids
      // getInterfaceInfo(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      // });
      this.form = JSON.parse(JSON.stringify(row));
      this.title = "修改企业查询调用接口信息";
      this.callInterfaceName = this.form.callInterfaceName;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            let that = this
            this.$dtModal.confirm('是否确认修改企业信息查询调用接口为"' + this.callInterfaceName + '"的数据项？').then(function() {
              updateInterfaceInfo(that.form).then(response => {
                that.$dtModal.msgSuccess("修改成功");
                that.open = false;
                that.getList();
              });
            }).catch(() => {});
          } else {
            addInterfaceInfo(this.form).then(response => {
              this.$dtModal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$dtModal.confirm('是否确认删除企业信息查询调用接口为"' + row.callInterfaceName + '"的数据项？').then(function() {
        return delInterfaceInfo(id);
      }).then(() => {
        this.getList();
        this.$dtModal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
